﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using StedySoft.SenseSDK;
using StedySoft.SenseSDK.DrawingCE;

namespace Transportr
{
    public partial class CtlLineDetails : UserControl
    {
        private LineDetailsModel model;

        public CtlLineDetails(int lnId)
        {
            InitializeComponent();
            this.listDetails = new SenseListControl()
            {
                Dock = System.Windows.Forms.DockStyle.Fill,
                FocusedItem = null,
                IsSecondaryScrollType = false,
                MinimumMovement = 15,
                Name = "listLines",
                ShowScrollIndicator = true,
                Springback = 0.35F,
                TabIndex = 1,
                ThreadSleep = 100,
                TopIndex = 0,
                Velocity = 0.9F
            };
            this.Controls.Add(this.listDetails);
            this.model = new LineDetailsModel();
            this.model.Updated += new UpdatedHandler(model_Updated);
            this.model.BeginUpdate(new Metadata.LineDetailFilter() { LineIds = new List<int>() { lnId } });
        }

        void model_Updated()
        {
            this.Invoke((MethodInvoker)delegate()
            {
                this.listDetails.Clear();
                this.Populate();
            });
        }

        void Populate()
        {
            this.listDetails.BeginUpdate();
            LineDetailsItemViewModel m = model.Items.First();
            /* Company and line number */
            SensePanelItem headerItm = new SensePanelItem()
            {
                PrimaryText = m.Company,
                SecondaryText = m.LineNumber,
                Thumbnail = m.LineIcon
            };
            headerItm.OnClick += delegate(object sender)
            {
                if (SenseAPIs.SenseMessageBox.Show(String.Format("Are you sure you want to add line {0} to your favorites?", m.LineNumber),
                                                   "Add to favorites",
                                                   SenseMessageBoxButtons.YesNo) == DialogResult.Yes)
                {
                    FavoriteStore.Store.Lines.Add(new FavoriteStore.LineDetails()
                    {
                        Brief = m.RawBrief,
                        Stations = m.RawStations,
                        Terms = m.RawTerms
                    });
                    FavoriteStore.Save();
                }
            };
            this.listDetails.AddItem(headerItm);
            /* Endstations */
            SensePanelItem routeItm = new SensePanelItem()
            {
                PrimaryText = m.CurrentRoute.First().Station.stName,
                SecondaryText = m.CurrentRoute.Last().Station.stName
            };
            routeItm.OnClick += delegate(object sender)
            {
                m.ReverseRoute = !m.ReverseRoute;
                model_Updated();
            };
            this.listDetails.AddItem(routeItm);
            /* Station selector */
            SensePanelComboItem stationItm = new SensePanelComboItem()
            {
                LabelText = "Select departing station"
            };
            foreach (StationViewModel st in m.CurrentRoute)
            {
                stationItm.Items.Add(new SensePanelComboItem.Item()
                {
                    Value = st,
                    Text = st.Name
                });
                stationItm.SelectedText = m.CurrentStation.Name;
            }
            stationItm.OnSelectedValueChanged += delegate(object Sender, object Value)
            {
                m.CurrentStation = Value as StationViewModel;
                model_Updated();
            };
            this.listDetails.AddItem(stationItm);
            /* Date selector */
            SensePanelDateTimeItem dtItm = new SensePanelDateTimeItem()
            {
                LabelText = "Select date",
                DateTime = m.CurrentDate
            };
            dtItm.OnDateTimeChanged += delegate(object Sender, DateTime DateTime)
            {
                m.CurrentDate = DateTime;
                model_Updated();
            };
            this.listDetails.AddItem(dtItm);

            foreach (TimetableItemViewModel t in m.Timetable)
            {
                bool selected = (dtItm.DateTime.Hour == t.Hour);
                this.listDetails.AddItem(new SensePanelItem()
                {
                    Enabled = selected,
                    Height = 30,
                    PrimaryText = String.Format("{0:00} | {1}", t.Hour, t.Record)
                });
            }
            this.listDetails.EndUpdate();
        }
    }
}
